package org.orbroker.callback

import java.util.logging._

/**
 * Callback for logging to [[scala.util.logging.Logged]].
 * @param thresholdMillis Only log statements taking longer than threshold
 * @param ignoreWarnings Set of SQL state codes to ignore when warned
 */
class ScalaLoggedCallback(thresholdMillis: Int = 0,
    ignoreWarnings: Set[String] = Set.empty) 
extends ExecutionCallback 
with LogEmbeddedValues
with scala.util.logging.Logged {
  def onSQL(id: Symbol, sql: String, parms: Seq[Any], executionTime: Int) {
    val millis = executionTime / 1000
    if (millis >= thresholdMillis) {
      val sqlWithValues = embedValues(sql.trim, parms)
      log("Executed '%s' taking %,d ms: %s".format(id.name, millis, sqlWithValues))
    }
  }

  def onSQLBatch(id: Symbol, sql: String, parms: Seq[Seq[Any]], executionTime: Int) {
    val millis = executionTime / 1000
    if (millis >= thresholdMillis) {
      log("Executed '%s' %,d times as batch taking %,d ms: %s".format(id.name, parms.size, millis, sql.trim))
    }
  }

  def onWarning(warning: java.sql.SQLWarning) = {
    if (!ignoreWarnings.contains(warning.getSQLState)) {
      log("[" + warning.getSQLState + "] " + warning.getMessage)
    }
  }

}
